package com.rcoming.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.BaseRequest;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import com.rcoming.R;
import com.rcoming.activity.PlayerActivity;
import com.rcoming.adapter.HotMovieAdapter;
import com.rcoming.api.Api;
import com.rcoming.base.BaseFragment;
import com.rcoming.model.HotMovieBean;
import com.rcoming.util.MyLogUtil;

import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends BaseFragment {


    @BindView(R.id.lv_hot_movie)
    RecyclerView lvHotMovie;
    @BindView(R.id.swipe_container)
    SwipeRefreshLayout swipeContainer;
    Unbinder unbinder;
    private List<HotMovieBean.EntriesBean> mList = new ArrayList<>();
    private HotMovieAdapter mHotMovieAdapter;


    public FirstFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_first, container, false);
        unbinder = ButterKnife.bind(this, view);
        lvHotMovie.setLayoutManager(new LinearLayoutManager(mActivity));
        mHotMovieAdapter = new HotMovieAdapter(mList);
        lvHotMovie.setAdapter(mHotMovieAdapter);


        swipeContainer.setDistanceToTriggerSync(400);
        swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        getData();
        return view;
    }

    private void getData() {
        OkGo.post("http://116.62.43.66:8080/liangguang-api/userIndex")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        MyLogUtil.d(s);
                    }
                });

        OkGo.get(Api.DouBan.Url_HotMovie)
                .params("apikey", Api.DouBan.DouBanApiKey)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        MyLogUtil.d(s);
                        Gson gson = new Gson();
                        HotMovieBean bean = gson.fromJson(s, HotMovieBean.class);
                        mList.addAll(bean.getEntries());
                        mHotMovieAdapter.setNewData(mList);
                        mHotMovieAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                startActivity(new Intent(getActivity(), PlayerActivity.class));
                            }
                        });
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        swipeContainer.setRefreshing(true);
                    }

                    @Override
                    public void onCacheSuccess(String s, Call call) {
                        super.onCacheSuccess(s, call);
                        Gson gson = new Gson();
                        HotMovieBean bean = gson.fromJson(s, HotMovieBean.class);
                        mList.addAll(bean.getEntries());
                        mHotMovieAdapter.setNewData(mList);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);
                        swipeContainer.setRefreshing(false);
                    }
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
